<template>
    <IContent :spinShow="loading">
        <ISearch v-model="search">
            <Row>
                <FormItem label="类型">
                    <RadioGroup v-model="search.type" type="button" size="small">
                        <Radio label="">全部</Radio>
                        <Radio label="0">一单一品</Radio>
                        <Radio label="1">一单多品</Radio>
                    </RadioGroup>
                </FormItem>
            </Row>
            <Row>
                <FormItem label="状态">
                    <RadioGroup v-model="search.status" type="button" size="small">
                        <Radio label="">全部</Radio>
                        <Radio label="0">草稿</Radio>
                        <Radio label="1">已分配</Radio>
                        <Radio label="2">已下架</Radio>
                    </RadioGroup>
                </FormItem>
            </Row>
            <FormItem label="下架单号">
                <Input size="small" v-model="search.code"></Input>
            </FormItem>
            <FormItem :label-width="1">
                <Button type="primary" icon="ios-search" @click="getLists(1)" size="small">搜索</Button>
            </FormItem>
        </ISearch>
        <ITable :current="page.current" :table="table" :total="page.total" @on-page-change="pageChange">
            <template slot-scope="{ row, index }" slot="type">
                <span v-if="row.type === 0">一单一品</span>
                <span v-if="row.type === 1">一单多品</span>
            </template>
            <template slot-scope="{ row, index }" slot="user">
                <span v-if="row.user === null">未分配</span>
                <span v-else>{{row.user.name}}</span>
            </template>
            <template slot-scope="{ row, index }" slot="status">
                <span v-if="row.status === 0">草稿</span>
                <span v-if="row.status === 1">已分配</span>
                <span v-if="row.status === 2">已下架</span>
            </template>
            <template slot-scope="{ row, index }" slot="action">
                <Button size="small" @click="openComponent('IIView', {id: row.id})">查看</Button>
                <Button size="small" v-if="row.status === 0" @click="openComponent('DISTRIBUTE', {id: row.id})">分配</Button>
                <PoptipButton size="small" v-if="row.status === 1" @on-ok="submitForPicking(row.id)" content="是否确认下架？">下架</PoptipButton>
            </template>
        </ITable>

        <component v-if="componentProps.value" :is="componentProps.view" v-model="componentProps.value" :props="componentProps.props"
                   @input="getLists(page.current)"></component>
    </IContent>
</template>

<script>
    import contentListPage from "../../../mixins/contentListPage";
    import IContent from "../../../components/content/index";
    import ISearch from "../../../components/content/search";
    import ITable from "../../../components/content/table";
    import contentDrawer from "../../../mixins/contentDrawer";
    import RemoteSelect from "../../../components/form/remoteSelect";
    import IPrint from './print';
    import DISTRIBUTE from './distribute';
    import IIView from './view';
    import PoptipButton from "../../../components/content/poptipButton";

    export default {
        name: "index",
        components: {PoptipButton, RemoteSelect, ITable, ISearch, IContent, IIView, IPrint, DISTRIBUTE},
        mixins: [contentListPage, contentDrawer],
        data(){
            return {
                table: {
                    columns: [
                        {
                            title: '下架单号',
                            key: 'code'
                        },
                        {
                            title: '下架单类型',
                            slot: 'type'
                        },
                        {
                            title: '订单数量',
                            key: 'count'
                        },{
                            title: '状态',
                            slot: 'status'
                        },{
                            title: '创建时间',
                            key: 'created_at'
                        },{
                            title: '分配人员',
                            slot: 'user'
                        },
                        {
                            title: '操作',
                            slot: 'action'
                        }
                    ]
                },
                search: {}
            }
        },
        methods: {
            getLists(page) {
                this.loading = true;
                this.$http.get(`order/picking`, {
                    params: Object.assign({}, this.search, {page: page})
                }).then((data) => {
                    this.table.data = data.data
                    this.page.total = data.total
                    this.page.current = data.current_page
                }).finally(() => {
                    this.loading = false;
                });
            },
            submitForPicking(id){
                this.loading = true;
                this.$http.put(`order/picking/${id}/picking`, this.data).then((data) => {
                    this.getLists(this.page.current);
                }).finally(() => {
                    this.loading = false;
                });
            }
        }
    }
</script>

<style scoped>

</style>
